package com.emergency.controller;

import com.emergency.data.param.StartResponseParam;
import com.emergency.http.WrapMapper;
import com.emergency.http.Wrapper;
import com.emergency.service.DepartmentService;
import com.emergency.service.EmergencyRecordingService;
import com.emergency.util.ShowUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

@Validated
@RestController
@RequestMapping("/response")
@Api(tags = "应急响应API")
public class ResponseController {

    @Resource
    private EmergencyRecordingService emergencyRecordingService;

    @Resource
    private DepartmentService departmentService;

    /**
     * 应急响应数据统计
     *
     * @return Wrapper
     */
    @ApiOperation("应急响应数据统计")
    @GetMapping("/dataStatistics")
    public Wrapper<?> dataStatistics() {
        return WrapMapper.ok(emergencyRecordingService.dataStatistics());
    }

    /**
     * 部门列表（用户类部门）
     *
     * @param level 部门级别 0-县级 1-镇级 2-村级
     * @return Wrapper
     */
    @ApiOperation("部门列表（用户类部门）")
    @GetMapping("/getDepartmentList")
    public Wrapper<?> getDepartmentList(@ApiParam("部门级别") @RequestParam(required = false) Integer level) {
        return WrapMapper.ok(departmentService.getDepartmentList(level));
    }

    /**
     * 当前启动响应状态
     *
     * @return Wrapper
     */
    @ApiOperation("当前启动响应状态")
    @GetMapping("/getCurrentLevel")
    public Wrapper<?> getCurrentLevel() {
        return WrapMapper.ok(emergencyRecordingService.getCurrentLevel());
    }

    /**
     * 应急响应部门确认数据
     *
     * @return Wrapper
     */
    @ApiOperation("应急响应部门确认数据")
    @GetMapping("/deptConfirmList")
    public Wrapper<?> deptConfirmList() {
        return WrapMapper.ok(emergencyRecordingService.deptConfirmList());
    }

    /**
     * 启动应急响应
     *
     * @param param 响应数据
     * @return Wrapper
     */
    @ApiOperation("启动应急响应")
    @PostMapping("/start")
    public Wrapper<?> start(@Validated @RequestBody StartResponseParam param) {
        emergencyRecordingService.start(param);
        return WrapMapper.ok();
    }

    /**
     * 调整应急响应
     *
     * @param param 响应数据
     * @return Wrapper
     */
    @ApiOperation("调整应急响应")
    @PostMapping("/change")
    public Wrapper<?> change(@Validated @RequestBody StartResponseParam param) {
        emergencyRecordingService.change(param);
        return WrapMapper.ok();
    }

    /**
     * 解除应急响应
     *
     * @param id 响应启动记录id
     * @return Wrapper
     */
    @ApiOperation("解除应急响应")
    @GetMapping("/stop")
    public Wrapper<?> stop(@ApiParam("响应启动记录id") @NotNull(message = "id 不能为空") @RequestParam Long id) {
        emergencyRecordingService.stop(id);
        return WrapMapper.ok();
    }

    /**
     * 确认应急响应应答
     *
     * @param id 响应确认id
     */
    @ApiOperation("确认应急响应应答")
    @GetMapping("/confirmRecording")
    public void confirmRecording(@ApiParam("响应确认id") @NotNull(message = "id 不能为空") @RequestParam Long id,
                                       HttpServletResponse response) {
        emergencyRecordingService.confirmRecording(id);
        ShowUtil.showPic(response);
    }
}
